#! /bin/sh
JAVA_HOME=/usr/local/jdk1.8.0_191

MODELS=(business basic dictionary chat gateway oss order xxl supervise)
JARS=(business.jar basic.jar dictionary.jar chat.jar gateway.jar oss.jar order.jar xxl.jar supervise.jar)
ENVS=('-Xms1024m -Xmx1024m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m' '-Xms512m -Xmx512m')
SCRIPT_DIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd)
cd $SCRIPT_DIR
JAR_PATH=$(pwd)
echo "$JAR_PATH"
start() {
  local JAR_NAME=
  local MODEL_NAME=
  local ENV=
  local command="$1"
  local commandOk=0
  local count=0
  local okCount=0
  for((i=0;i<${#MODELS[@]};i++))
  do
	MODEL_NAME=${MODELS[$i]}
	JAR_NAME=${JARS[$i]}
  ENV=${ENVS[$i]}
    if [ -f "$JAR_PATH/$JAR_NAME" ] && ([ "$command" == "all" ] || [ "$command" == "$MODEL_NAME" ]);then
      commandOk=1
      count=0
      PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
      if [ -n "$PID" ];then
        echo "$JAR_NAME:starting,PID=$PID"
      else
        exec nohup $JAVA_HOME/bin/java $ENV -jar $JAR_PATH/$JAR_NAME >/dev/null 2>&1 &
        PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        while [ -z "$PID" ]
        do
          if (($count == 30));then
            echo "$JAR_NAME:$(expr $count \* 10)error!"
            break
          fi
          count=$(($count+1))
          echo "$JAR_NAME starting.................."
          sleep 10s
          PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        done
        okCount=$(($okCount+1))
        echo "$JAR_NAME:start success,PID=$PID"
      fi
    fi
  done
  if(($commandOk == 0));then
    echo "second parameter error"
  else
    echo "............start:$okCount service..........."
  fi
}
 
stop() {
  local JAR_NAME=
  local MODEL_NAME=
  local command="$1"
  local commandOk=0
  local okCount=0
  for((i=0;i<${#MODELS[@]};i++))
  do
	MODEL_NAME=${MODELS[$i]}
	JAR_NAME=${JARS[$i]}
    
    if [ -f "$JAR_PATH/$JAR_NAME" ] && ([ "$command" == "all" ] || [ "$command" == "$MODEL_NAME" ]);then
      commandOk=1
      PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
      if [ -n "$PID" ];then
        echo "$JAR_NAME:stop,PID=$PID"
        kill -9 $PID
        PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        while [ -n "$PID" ]
        do
          sleep 3s
          PID=`ps -ef |grep $(echo $JAR_NAME | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
        done
        echo "$JAR_NAME:stop success"
        okCount=$(($okCount+1))
      else
        echo "$JAR_NAME:not runing"
      fi
    fi
  done
  if (($commandOk == 0));then
    echo "second parameter error"
  else
    echo "............stop:$okCount service............"
  fi
}
 
 
case "$1" in
  start)
    start "$2"
  ;;
  stop)
    stop "$2"
  ;;
  restart)
    stop "$2"
    sleep 3s
    start "$2"
  ;;
  *)
    echo "first parameter error :start|stop|restart"
    exit 1
  ;;
esac
